﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Web;
using System.Web.SessionState;

namespace jnxc.Web.harmfulinfo
{
    /// <summary>
    /// GetHarm 的摘要说明
    /// </summary>
    public class GetHarm : IHttpHandler, IRequiresSessionState
    {

        StringBuilder result = new StringBuilder();
        StringBuilder sb = new StringBuilder();
        StringBuilder rechild = new StringBuilder();
        jnxc.BLL.harmfulinfo bll = new BLL.harmfulinfo();
        public void ProcessRequest(HttpContext context)
        {
            string ac = "";
            if (null != context.Request.Form["action"])
            {//获取前台传来的值  
                ac = context.Request.Form["action"].ToString().Trim();
            }
            else if (null != context.Request.QueryString["action"])
            {
                ac = context.Request.QueryString["action"].ToString().Trim();
            }
            
            if (ac == "querypro")
                Querypro(context);//本省数据
            else if (ac == "query")
                Query(context);//全 国数据
            else if (ac == "querytg")
                QueryGridTree(context); //测试          
            else if (ac == "add")
                add(context);
            else if (ac == "modify")
                modify(context);
            else if (ac == "delete")
                delete(context);
            else if (ac == "queryc")
                Querychild(context);
            else if (ac == "queryr")
                QueryRoot(context);
            else if (ac == "querymy")
                Querymy(context);
            else if (ac == "addmynode")
                addmy(context);
            else if (ac == "deletemy")
                deletemy(context);


        }
        public void deletemy(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            if (Web.Admin.myinfo.isLogin(context))
            { 
            int uid = 0;
            string uidstr = context.Request.QueryString["mhid"];
            if (uidstr != "")
                uid = int.Parse(uidstr);
            jnxc.BLL.myharmful mybll = new BLL.myharmful();
            if (mybll.Delete(uid))
            {
                context.Response.Write("T");//返回给前台页面  
                context.Response.End();
            }
            else
            {
                context.Response.Write("F");//返回给前台页面  
                context.Response.End();

            }
            }

        }
        public void Querychild(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            string pro = Web.Admin.myinfo.getPCC(context).Split(',')[0];
            string pid = context.Request.QueryString["pid"];
            StringBuilder strWhere = new StringBuilder();
            if (pro != "")
            {
                strWhere.AppendFormat(" province='{0}'  ", pro);
            }
            DataSet ds = bll.GetList(strWhere.ToString());

            string str = GetTreechild(ds.Tables[0], "hid", "parentid", pid);
            if (str == "")
                str = pid;
            else
                str += pid;
            string json = "[{\"ids\":\"" + str + "\"}]";
            context.Response.Write(json);//返回给前台页面  
            context.Response.End();

        }
        private string GetTreechild(DataTable tabel, string idCol, string rela, object pId)
        {
            rechild.Append(sb.ToString());
            sb.Remove(0, sb.Length);
            if (tabel.Rows.Count > 0)
            {
                string filer = string.Format("{0}='{1}'", rela, pId);
                DataRow[] rows = tabel.Select(filer);
                if (rows.Length > 0)
                {
                    foreach (DataRow row in rows)
                    {
                        sb.Append(row[idCol] + ",");
                        if (tabel.Select(string.Format("{0}='{1}'", rela, row[idCol])).Length > 0)
                        {
                            GetTreechild(tabel, idCol, rela, row[idCol]);
                            rechild.Append(sb.ToString());
                            sb.Remove(0, sb.Length);
                        }
                        rechild.Append(sb.ToString());
                        sb.Remove(0, sb.Length);
                    }
                    //sb = sb.Remove(sb.Length - 1, 1);
                }
                rechild.Append(sb.ToString());
                sb.Remove(0, sb.Length);
            }
            return rechild.ToString();
        }
        public void delete(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            int uid = 0;
            string uidstr = context.Request.QueryString["hid"];
            if (uidstr != "")
                uid = int.Parse(uidstr);
            if (bll.Delete(uid))
            {
                context.Response.Write("T");//返回给前台页面  
                context.Response.End();
            }
            else
            {
                context.Response.Write("F");//返回给前台页面  
                context.Response.End();

            }

        }
        public void modify(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            
            string province = Web.Admin.myinfo.getPCC(context).Split(',')[0];
            if (province != "")
            {
                decimal r = 0.00M;
                string hname = context.Request.Form["hname"];
                string hbm = context.Request.Form["hbm"];
                int hid = int.Parse(context.Request.Form["hid"]);
                string ldxm = context.Request.Form["ldxm"];
                string rstr = context.Request.Form["rvalue"];
                if (rstr != "") r = decimal.Parse(rstr);
                jnxc.Model.harmfulinfo model = new jnxc.Model.harmfulinfo();
                model.hname = hname;
                model.hbm = hbm;
                model.hid = hid;
                model.ldxm = ldxm;
                model.rvalue = r;
                if (bll.Update(model))
                {
                    context.Response.Write("T");//返回给前台页面  
                    context.Response.End();

                }
                else
                {
                    context.Response.Write("F");//返回给前台页面  
                    context.Response.End();
                }
            }
            else
            {
                context.Response.Write("L");//返回给前台页面  
                context.Response.End();
            }
        }
        public void add(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            
            string province = Web.Admin.myinfo.getPCC(context).Split(',')[0];
            if (province != "")
            {
                decimal r=0.00M;
                string hname = context.Request.Form["hname"];
                string hbm = context.Request.Form["hbm"];
                string ldxm = context.Request.Form["ldxm"];
                string rstr = context.Request.Form["rvalue"];
                int parentid = 0;
                if (context.Request.Form["parentid"] != "")
                    parentid = int.Parse(context.Request.Form["parentid"]);
                if (rstr != "") r = decimal.Parse(rstr);
                jnxc.Model.harmfulinfo model = new jnxc.Model.harmfulinfo();
                model.hname = hname;
                model.parentid = parentid;
                model.hbm = hbm;
                model.ldxm = ldxm;
                model.rvalue = r;
                //model.province = province;
                if (bll.Add(model))
                {
                    context.Response.Write("T");//返回给前台页面  
                    context.Response.End();

                }
                else
                {
                    context.Response.Write("F");//返回给前台页面  
                    context.Response.End();
                }
            }
            else
            {
                context.Response.Write("L");//返回给前台页面  
                context.Response.End();
            }

        }
        public void addmy(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            int hid = 0;
            string hids = context.Request.QueryString["hid"];
            if (hids != "") hid = int.Parse(hids);
            if (hid != 0)
            {
            string province = Web.Admin.myinfo.getPCC(context).Split(',')[0];
            int uid = Web.Admin.myinfo.getID(context);
            if (province != "")
            {                
                jnxc.Model.myharmful model = new jnxc.Model.myharmful();
                model.hid = hid;
                model.uid =uid;
                model.province = province;
                jnxc.BLL.myharmful mybll = new BLL.myharmful();
                if (mybll.Add(model)>0)
                {
                    context.Response.Write("T");//返回给前台页面  
                    context.Response.End();

                }
                else
                {
                    context.Response.Write("F");//返回给前台页面  
                    context.Response.End();
                }
            }
            else
            {
                context.Response.Write("L");//返回给前台页面  
                context.Response.End();
            }
            }

        }
        private string GetTreeJsonByTable(DataTable tabel, string idCol, string txtCol, string rela, object pId)
        {
            result.Append(sb.ToString());
            sb.Remove(0, sb.Length);
            if (tabel.Rows.Count > 0)
            {
                sb.Append("[");
                string filer = string.Format("{0}='{1}'", rela, pId);
                DataRow[] rows = tabel.Select(filer);
                if (rows.Length > 0)
                {
                    foreach (DataRow row in rows)
                    {
                        sb.Append("{\"id\":\"" + row[idCol] + "\",\"text\":\"" + row[txtCol] + "\",\"bm\":\"" + row["hbm"] + "\",\"ldxm\":\"" + row["ldxm"] + "\",\"rvalue\":\"" + row["rvalue"] + "\"");
                        if (tabel.Select(string.Format("{0}='{1}'", rela, row[idCol])).Length > 0)
                        {
                            //点击展开
                            sb.Append(",\"children\":");
                            GetTreeJsonByTable(tabel, idCol, txtCol, rela, row[idCol]);
                            result.Append(sb.ToString());
                            sb.Remove(0, sb.Length);
                        }
                        result.Append(sb.ToString());
                        sb.Remove(0, sb.Length); ;
                        sb.Append("},");
                    }
                    sb = sb.Remove(sb.Length - 1, 1);
                }
                sb.Append("]");
                result.Append(sb.ToString());
                sb.Remove(0, sb.Length);
            }
            return result.ToString();
        }
        private string GetTreeJsonByTableOne(DataTable tabel, string idCol, string txtCol, string bmCol)
        {
            result.Remove(0, result.Length);
            if (tabel.Rows.Count > 0)
            {
                result.Append("[");

                foreach (DataRow row in tabel.Rows)
                {
                    result.Append("{\"id\":\"" + row[idCol] + "\",\"text\":\"" + row[txtCol] + "\",\"bm\":\"" + row[bmCol] + "\"},");
                }
                result = result.Remove(result.Length - 1, 1);

                result.Append("]");

            }
            return result.ToString();
        }
        public void Query(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            if (Web.Admin.myinfo.isLogin(context))
            {
            //string pro = Web.Admin.myinfo.getPCC(context).Split(',')[0];

            //StringBuilder strWhere = new StringBuilder();
            //if (pro != "")
            //{
            //    strWhere.AppendFormat(" province='{0}'  ", pro);
            //}
            DataSet ds = bll.GetList(" 1=1 order by hbm");

            string str = GetTreeJsonByTable(ds.Tables[0], "hid", "hname", "parentid", 0);
            context.Response.Write(str);//返回给前台页面  
            context.Response.End();
            }
        }
        public void Querymy(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            if (Web.Admin.myinfo.isLogin(context))
            {
                jnxc.BLL.myharmful mybll = new BLL.myharmful();
                int uid = Web.Admin.myinfo.getID(context);
                DataSet ds = mybll.GetListv(" uid="+uid);
                string str = GetTreeJsonByTableOne(ds.Tables[0], "mhid", "hname", "hbm");
                context.Response.Write(str);//返回给前台页面  
                context.Response.End();
            }
        }
        public void Querypro(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            if (Web.Admin.myinfo.isLogin(context))
            {
                string pro = Web.Admin.myinfo.getPCC(context).Split(',')[0];

                StringBuilder strWhere = new StringBuilder();
                if (pro != "")
                {
                    strWhere.AppendFormat(" province='{0}'  ", pro);
                }
                jnxc.BLL.myharmful mybll = new BLL.myharmful();                
                DataSet ds = mybll.GetListv(strWhere.ToString());
                string str = GetTreeJsonByTableOne(ds.Tables[0], "mhid", "hname", "hbm");
                context.Response.Write(str);//返回给前台页面  
                context.Response.End();
            }
        }
        public void QueryRoot(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            string pro = Web.Admin.myinfo.getPCC(context).Split(',')[0];

            StringBuilder strWhere = new StringBuilder();
            if (pro != "")
            {
                strWhere.AppendFormat(" province='{0}'  ", pro);
            }
            jnxc.BLL.myharmful mybll = new BLL.myharmful(); 
            DataSet ds = mybll.GetListv(strWhere.ToString());

            string str = GetTreeJsonByTableOne(ds.Tables[0], "mhid", "hname", "hbm");
            str = "[{\"id\":0,\"text\":\"全部\",\"children\":" + str + "}]";
            context.Response.Write(str);//返回给前台页面  
            context.Response.End();

        }
        public void QueryGridTree(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            if (Web.Admin.myinfo.isLogin(context))
            {                
                DataSet ds = bll.GetList("");
                //string str = ToJson.Dataset2Json(ds, ds.Tables[0].Rows.Count, 50);
                string str = jnxc.Common.JsonHelper.DataTable2TreeJson("hid", "hname", "parentid", 0, ds.Tables[0], 0);
                context.Response.Write(str);//返回给前台页面  
                context.Response.End();
            }
        }
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}